package com.example.xViewChat.Util;

import com.example.xViewChat.Bean.testBean;

import java.io.*;

/**
 * Created by hasee on 2016-04-06.
 */
public class SerializeUtil {
    /**
     * @Author ��Ȼ�޷����л����Ǿ����˰ɡ���һ�ַ�ʽ��
     */
    public static byte[] serialize(Object object) {
        ObjectOutputStream objectOutputStream = null;
        ByteArrayOutputStream byteArrayOutputStream = null;


        try {
            byteArrayOutputStream = new ByteArrayOutputStream();
            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(object);
            byte[] bytes = byteArrayOutputStream.toByteArray();
            return bytes;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Object unserialize(byte[] bytes) {
        ByteArrayInputStream byteArrayInputStream = null;
        try {
            byteArrayInputStream = new ByteArrayInputStream(bytes);
            ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
            return objectInputStream.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] argc) {
        testBean t = new testBean();
        byte[] bytes = SerializeUtil.serialize(t);
        System.out.println(t);
        System.out.println(bytes.toString());

        testBean xx = (testBean) SerializeUtil.unserialize(bytes);
        System.out.println(xx);
    }
}
